package com.skyable.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skyable.account.entity.SysPermission;
import com.skyable.account.service.IPermissionService;
import com.skyable.account.mapper.PermissionMapper;
import com.skyable.common.common.ServiceResult;
import com.skyable.common.constants.exception.AccountExcpConstants;
import com.skyable.common.constants.param.CommonParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author GaoYuan
 * @title: PermissionServiceImpl
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/129:22
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,SysPermission> implements IPermissionService {

    @Override
    public ServiceResult<SysPermission> insertPermission(SysPermission sysPermission) {
        ServiceResult<SysPermission> serviceResult = new ServiceResult<>();
        SysPermission permission = lambdaQuery().eq(SysPermission::getPermissionCode, sysPermission.getPermissionCode()).one();
        if(permission != null) {
            serviceResult.setError(AccountExcpConstants.ERROR_CODE_PERMISSION_EXISTS,AccountExcpConstants.ERROR_MSG_PERMISSION_EXISTS);
            return serviceResult;
        }
        sysPermission.setCreateTime(new Date());
        sysPermission.setUpdateTime(new Date());
        baseMapper.insert(sysPermission);
        return serviceResult;
    }

    @Override
    public ServiceResult<SysPermission> updatePermission(SysPermission sysPermission) {
        ServiceResult<SysPermission> serviceResult = new ServiceResult<>();
        SysPermission permission = lambdaQuery().ne(SysPermission::getId,sysPermission.getId()).eq(SysPermission::getPermissionCode, sysPermission.getPermissionCode()).one();
        if(permission != null) {
            serviceResult.setError(AccountExcpConstants.ERROR_CODE_PERMISSION_EXISTS,AccountExcpConstants.ERROR_MSG_PERMISSION_EXISTS);
            return serviceResult;
        }
        baseMapper.updateById(sysPermission);
        return serviceResult;
    }

    @Override
    public ServiceResult<IPage<SysPermission>> findPermissionListByPage(int page, int limit, SysPermission sysPermission) {
        ServiceResult<IPage<SysPermission>> serviceResult = new ServiceResult<>();
        Page<SysPermission> sysPermissionPage = new Page<>(page, limit);
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sysPermission);
        queryWrapper.orderByAsc(CommonParams.ORDER_NUM);
        IPage<SysPermission> permissionIPage = baseMapper.selectPage(sysPermissionPage, queryWrapper);
        serviceResult.setResult(permissionIPage);
        return serviceResult;
    }

    @Override
    public ServiceResult<List<SysPermission>> findPermissionList() {
        ServiceResult<List<SysPermission>> serviceResult = new ServiceResult<>();
        List<SysPermission> sysPermissionList = lambdaQuery().orderByAsc(SysPermission::getOrderNum).list();
        serviceResult.setResult(sysPermissionList);
        return serviceResult;
    }

    @Override
    public ServiceResult<List<SysPermission>> findPermissionByUserName(String userName) {
        ServiceResult<List<SysPermission>> serviceResult = new ServiceResult<>();
        List<SysPermission> sysPermissionList = baseMapper.findPermissionByUserName(userName);
        serviceResult.setResult(sysPermissionList);
        return serviceResult;
    }

    @Override
    public ServiceResult<SysPermission> findPermissionById(Long permissionId) {
        ServiceResult<SysPermission> serviceResult = new ServiceResult<>();
        SysPermission sysPermission = lambdaQuery().eq(SysPermission::getId,permissionId).one();
        serviceResult.setResult(sysPermission);
        return serviceResult;
    }

    @Override
    public ServiceResult<Integer> deletePermissionById(Long permissionId) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        int result = baseMapper.deleteById(permissionId);
        serviceResult.setResult(result);
        return serviceResult;
    }
}
